// 
// Decompiled by Procyon v0.5.36
// 

package com.orange.boot.dev1.service.impl;

import com.orange.boot.utils.IpAndPortUtil;
import java.io.IOException;
import java.io.File;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.boot.utils.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.orange.boot.dev1.service.EffectService;
import com.orange.boot.dev1.entity.Effect;
import com.orange.boot.dev1.mapper.EffectMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class EffectServiceImpl extends ServiceImpl<EffectMapper, Effect> implements EffectService {

    @Value("${uploadPath}")
    String uploadPath;

    public EffectServiceImpl() {
    }

    public R listPage(Long current, Integer limit, String like) {
        Page<Effect> page = new Page(current, (long)limit);
        QueryWrapper<Effect> wrapper = new QueryWrapper();
        if (like != null) {
            wrapper.like("name", like);
        }

        Page<Effect> returnPage = (Page)((EffectMapper)this.baseMapper).selectPage(page, wrapper);
        long total = returnPage.getTotal();
        List<Effect> effects = returnPage.getRecords();
        return R.ok().data("total", total).data("rows", effects);
    }

    public R addEffect(String name, String effectDetails, MultipartFile pic) {
        if (pic != null) {
            String fileName = System.currentTimeMillis() + pic.getOriginalFilename();
            System.out.println(this.uploadPath);
            File dir = new File(this.uploadPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            File targetFile = new File(this.uploadPath, fileName);

            try {
                pic.transferTo(targetFile);
            } catch (IOException var10) {
                var10.printStackTrace();
                return R.error().message("上传图片失败");
            }

            String ipAndPort = IpAndPortUtil.getIpAndPort();
            String picUrl = "http://" + ipAndPort + "/news/" + fileName;
            Effect effect = new Effect();
            effect.setName(name);
            effect.setEffectDetails(effectDetails);
            effect.setImg(picUrl);
            ((EffectMapper)this.baseMapper).insert(effect);
            return R.ok();
        } else {
            return R.error().message("上传的文件为空！");
        }
    }
}